package com.qd.panda.invoice;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.invoice.UserInvoiceRecordsDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.invoice.UserInvoiceRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 发票/WEB_开票信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/invoice/userInvoiceRecords/")
@Slf4j
public class WebUserInvoiceRecordsController {
    private final UserInvoiceRecordsService userInvoiceRecordsService;

    /**
     * 处理
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:userInvoiceRecords:handle')")
    @OperatorInfo(title = "处理", operator = OperatorType.UPDATE)
    @PutMapping(value = "/handle")
    public ApiResultWrapper<Object> handle(@Validated @RequestBody UserInvoiceRecordsDTO dto) {
        if (!StringUtils.hasLength(dto.getLogId())) {
            return ApiResult.error("记录ID值错误");
        }
        if (null == dto.getStatus()) {
            return ApiResult.error("状态不能为空");
        }
        if (dto.getStatus() != 1 && dto.getStatus() != 2) {
            return ApiResult.error("状态值错误");
        }
        if (dto.getStatus() == 2) {
            if (!StringUtils.hasLength(dto.getRemarks())) {
                return ApiResult.error("请填写驳回原因");
            }
        }
        userInvoiceRecordsService.handle(dto);
        dto.freeData();
        return ApiResult.success("处理成功");
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserInvoiceRecordsDTO> page(UserInvoiceRecordsDTO dto) {
        return userInvoiceRecordsService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 订单列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/orderList")
    public ApiResultWrapper<Object> orderList(UserInvoiceRecordsDTO dto) {
        if (!StringUtils.hasLength(dto.getLogId())) {
            return ApiResult.error("记录ID不能为空");
        }
        return ApiResult.success(userInvoiceRecordsService.orderList(dto.getLogId()));
    }
}